%% This code plots Fiscal Impulse Responses
clearvars -except Globaloption option

if exist('Globaloption','var')==0
    Globaloption.savefig=1;
end

load MAT/res_annual_VAR_nodebt_cy.mat;

shocks = [gdppos, dtpos, dgpos];
shock_size = [-0.01,log(1-0.01/mean(taxrevgdp)),log(1+0.01/mean(spendgdp))];
lent = 16;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Simulation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
run ../../tools/IR_Simulation.m

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% IRF
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
zname = ['x', 't', 'g'];

scales = [.3, 1.5, 1.5];

for i = 1:3

    if i == 1
        z_coint1pos = z1_coint1pos;
        z_coint2pos = z1_coint2pos;
        z_spos = z1_spos;
    elseif i == 2
        z_coint1pos = z2_coint1pos;
        z_coint2pos = z2_coint2pos;
        z_spos = z2_spos;
    elseif i == 3
        z_coint1pos = z3_coint1pos;
        z_coint2pos = z3_coint2pos;
        z_spos = z3_spos;
    end

    f = figure;
    f.Position = [100 100 1000 200];

    z = zeros(lent, N);
    z(2, :) = Sig(:, shocks(i))' / Sig(shocks(i), shocks(i)) * shock_size(i);

    for t = 3:lent
        z(t, :) = Psi * z(t - 1, :)';
    end

    spos = N + 1;
    z(:, spos) = mean(taxrevgdp) * exp(z(:, coint_tax)) - mean(spendgdp) * exp(z(:, coint_spending));

    Ax = subplot(1, 3, 1);
    plot(1:lent - 1, zeros(1, lent - 1), 'k');
    hold on
    plot(1:lent - 1, (mean(taxrevgdp) * (exp(z(2:lent, coint_tax)) - 1)) * 100, 'black', 'LineWidth', 3)
    ylim([-scales(i), scales(i)]);
    xlim([1, lent - 1]);
    ylabel('Tax/GDP (%)');
    set(gca, 'Layer', 'top', 'FontSize', 7)
    set(gca, 'FontName', 'Times New Roman')
    grid

    hold on
    err1 = [prctile(z_coint1pos', normcdf(1) * 100); prctile(z_coint1pos', normcdf(-1) * 100)];
    err2 = [prctile(z_coint1pos', normcdf(2) * 100); prctile(z_coint1pos', normcdf(-2) * 100)];
    patch([1:lent - 1, fliplr(1:lent - 1)], [err1(1, :), fliplr(err1(2, :))], 'black', ...
        'FaceAlpha', 0.1, 'EdgeColor', 'none') % Shaded Confidence Intervals
    patch([1:lent - 1, fliplr(1:lent - 1)], [err2(1, :), fliplr(err2(2, :))], [61, 157, 214] / 255, ...
        'FaceAlpha', 0.1, 'EdgeColor', 'none') % Shaded Confidence Intervals
    hold off

    Ax = subplot(1, 3, 2);
    plot(1:lent - 1, zeros(1, lent - 1), 'k');
    hold on
    plot(1:lent - 1, (mean(spendgdp) * (exp(z(2:lent, coint_spending)) - 1)) * 100, 'black', 'LineWidth', 3)
    ylim([-scales(i), scales(i)]);
    xlim([1, lent - 1]);
    ylabel('Spending/GDP (%)');
    set(gca, 'Layer', 'top', 'FontSize', 7)
    set(gca, 'FontName', 'Times New Roman')
    grid

    hold on
    err1 = [prctile(z_coint2pos', normcdf(1) * 100); prctile(z_coint2pos', normcdf(-1) * 100)];
    err2 = [prctile(z_coint2pos', normcdf(2) * 100); prctile(z_coint2pos', normcdf(-2) * 100)];
    patch([1:lent - 1, fliplr(1:lent - 1)], [err1(1, :), fliplr(err1(2, :))], 'black', ...
        'FaceAlpha', 0.1, 'EdgeColor', 'none') % Shaded Confidence Intervals
    patch([1:lent - 1, fliplr(1:lent - 1)], [err2(1, :), fliplr(err2(2, :))], [61, 157, 214] / 255, ...
        'FaceAlpha', 0.1, 'EdgeColor', 'none') % Shaded Confidence Intervals
    hold off

    Ax = subplot(1, 3, 3);
    plot(1:lent - 1, zeros(1, lent - 1), 'k');
    hold on
    plot(1:lent - 1, (z(2:lent, spos) - z(1, spos)) * 100, 'black', 'LineWidth', 3)
    ylim([-scales(i), scales(i)]);
    xlim([1, lent - 1]);
    ylabel('Surplus/GDP (%)');
    set(gca, 'Layer', 'top', 'FontSize', 7)
    set(gca, 'FontName', 'Times New Roman')
    grid

    hold on
    err1 = [prctile(z_spos', normcdf(1) * 100); prctile(z_spos', normcdf(-1) * 100)];
    err2 = [prctile(z_spos', normcdf(2) * 100); prctile(z_spos', normcdf(-2) * 100)];
    patch([1:lent - 1, fliplr(1:lent - 1)], [err1(1, :), fliplr(err1(2, :))], 'black', ...
        'FaceAlpha', 0.1, 'EdgeColor', 'none') % Shaded Confidence Intervals
    patch([1:lent - 1, fliplr(1:lent - 1)], [err2(1, :), fliplr(err2(2, :))], [61, 157, 214] / 255, ...
        'FaceAlpha', 0.1, 'EdgeColor', 'none') % Shaded Confidence Intervals
    hold off

    f.PaperSize = [12 3];

    if Globaloption.savefig == 1

        switch i
            case 1
                set(gcf, 'PaperPositionMode', 'auto')
                print('../../results/figures_main/UK/pre1946/3CI_x_annual_before1946', '-dpng', '-r1200');
            case 2
                print('../../results/figures_main/UK/pre1946/3CI_dtau_annual_before1946', '-dpng', '-r1200');
            case 3
                print('../../results/figures_main/UK/pre1946/3CI_dg_annual_before1946', '-dpng', '-r1200');
        end

    end

end
